package cc.ucanuup.web.base.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cc.ucanuup.config.security.util.AppUtil;
import cc.ucanuup.config.security.util.UserInfo;
import cc.ucanuup.config.security.util.ValidateCodeHandle;
import cc.ucanuup.config.security.util.VerifyCodeUtils;
import cc.ucanuup.service.base.dto.MenuDto;
import cc.ucanuup.service.base.inter.UpMenuservice;

/**
 * Description: TODO[描述该类概要功能介绍]
 *
 * @author guwenbin
 * @date 2018年2月1日
 * @version 1.0
 */
@Controller
public class UserLoginController {
	/**
	 * 日志包
	 */
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Value("${web.path}")
	private String  webPath ;

	@Autowired
	private UpMenuservice  upMenuservice;


	@RequestMapping("/authImage")
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		HttpSession session = request.getSession(true);
		//生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		//存入会话session
		ValidateCodeHandle.save(session.getId(), verifyCode.toLowerCase());
		session.setAttribute("rand", verifyCode.toLowerCase());
		//生成图片
		int w = 200, h = 80;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
	}

	/**
	 * @Method: com.bozhon.srm.web.base.controller.SrmLoginController.srmlogin
	 * @Description: 后台管理员登陆的页面
	 * @author: WenBin
	 * @date: 2018年2月8日
	 * @version: 1.0
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/srmlogin")
	public String srmlogin(){
		return "base/login/bozhon-srm-login-admin";
	}

	/**
	 * @Method: com.bozhon.srm.web.base.controller.SrmLoginController.mainPage
	 * @Description: 这里是登陆的主页面
	 * @author: WenBin
	 * @date: 2018年2月8日
	 * @version: 1.0
	 * @param model
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping({"/",""})
	public String mainPage(Model model){
		UserInfo info = AppUtil.getSrmUserInfo();
		logger.info("主页面登陆-----------------------------------------------------");
		List<MenuDto> menus = upMenuservice.findAllMenusByUserAccount(info.getUserAccount());
		// ===================================================
		model.addAttribute("menus", menus);
		model.addAttribute("mainPage_user", info.getUserName()+"/"+info.getUserAccount());
		model.addAttribute("mainPage_control",!info.isSpLogin() ? null : "权限:("+info.getSpName()+"/"+info.getSpCode()+")");
		model.addAttribute("redirect",!(info.getLoginPage()!=null && info.getLoginPage().equals("2")) ? webPath+"/srmloginout" :  webPath+"/srmloginout?type=1");
		logger.info("主页面登陆完毕-------------------------------------------------------");
		return "base/login/bozhon-srm-home";
	}

	/**
	 * @Method: com.bozhon.srm.web.base.controller.SrmLoginController.login
	 * @Description: 默认的登陆页面
	 * @author: WenBin
	 * @date: 2018年2月8日
	 * @version: 1.0
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/login")
	public String login(){
		return "base/login/login2";
	}

	@RequestMapping("/helloSrm")
	public String hello() throws Exception{
		return "base/login/download1.1";
	}

	@RequestMapping("/srmloginout")
	public String srmloginout(String type,HttpSession session) throws Exception{
		session.invalidate();
		if(type!=null && type.equals("1")){
			return "redirect:/srmlogin";
		}
		return "redirect:/login";
	}
}
